﻿var TyksUserSelector = {
	getView : function(e, oldrybh, oldryxm, c, d) {
		var a = this.initPanel(c, oldrybh, oldryxm);
		var b = new Ext.Window({
					title : "选择用户",
					iconCls : "menu-appuser",
					width : 640,
					height : 480,
					layout : "fit",
					border : false,
					closeAction : "hide",
					items : [a],
					resizable : true,
					modal : true,
					buttonAlign : "center",
					buttons : [{
						text : "确认",
						iconCls : "btn-save",
						scope : "true",
						handler : function() {
							var l = "";
							var j = "";
							if (c) {
								var h = Ext.getCmp("contactGrid");
								var k = h.getSelectionModel().getSelections();
								for (var g = 0; g < k.length; g++) {
									if (g > 0) {
										l += ",";
										j += ",";
									}
									l += k[g].data.rybh;
									j += k[g].data.ryxm;
								}
							} else {
								var f = Ext.getCmp("selectedUserGrid")
										.getStore();
								for (var g = 0; g < f.getCount(); g++) {
									if (g > 0) {
										l += ",";
										j += ",";
									}
									l += f.getAt(g).data.rybh;
									j += f.getAt(g).data.ryxm;
								}
							}
							if (e != null) {
								e.call(this, l, j, f.getCount());
							}
							b.close();
						}
					}, {
						text : "关闭",
						iconCls : "btn-cancel",
						handler : function() {
							b.close();
						}
					}]
				});
		return b;
	},
	initPanel : function(e, oldrybh, oldryxm) {
		var i = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : _ctxPath + "/tyksUser4Select.do"
							}),
					reader : new Ext.data.JsonReader({
								root : "list",
								totalProperty : "totalCount",
								id : "id",
								fields : ['ryxm', 'rybh', 'wcl', 'yymc', 'bmqc']
							}),
					remoteSort : true
				});
		i.setDefaultSort("id", "desc");
		i.load({
					params : {
						start : 0,
						limit : 12
					}
				});
		var c = null;
		if (e) {
			c = new Ext.grid.CheckboxSelectionModel({
						singleSelect : true
					});
		} else {
			c = new Ext.grid.CheckboxSelectionModel();
		}
		var j = new Ext.grid.ColumnModel({
					columns : [c, new Ext.grid.RowNumberer(), {
						header : "姓名",
						dataIndex : 'ryxm',
						renderer : function(value, meta, record) {
							// var title=record.data.title;
							// if(title==1){
							return '<img src="' + _ctxPath
									+ '/images/flag/man.png"/>&nbsp;' + value;
							// }else{
							// return '<img
							// src="'+_ctxPath+'/images/flag/women.png"/>&nbsp;'+value;
							// }
						},
						width : 70
					}, {
						header : "完成率",
						dataIndex : 'wcl',
						width : 40
					}, {
						header : "专业",
						dataIndex : 'yymc',
						width : 40
					}, {
						header : "部门名称",
						dataIndex : 'bmqc',
						width : 250
					}],
					defaults : {
						sortable : true,
						menuDisabled : true,
						width : 120
					},
					listeners : {
						hiddenchange : function(n, o, p) {
							saveConfig(o, p);
						}
					}
				});
		var d = new Ext.tree.TreePanel({
					id : "treePanels",
					autoScroll : true,
					title : "按部门分类 ",
					iconCls : "dep-user",
					loader : new Ext.tree.TreeLoader({
								url : _ctxPath + "/genDeptTree.do"
							}),
					root : new Ext.tree.AsyncTreeNode({
								id : 34000000,
								expanded : true
							}),
					rootVisible : false,
					listeners : {
						"click" : this.clickNode
					}
				});
		var yylxPanel = new Ext.tree.TreePanel({
					id : 'yylxPanel',
					title : '按学法类型分类 ',
					autoScroll : true,
					loader : new Ext.tree.TreeLoader({
								url : _ctxPath + '/genXtyylxTree.do'
							}),
					root : new Ext.tree.AsyncTreeNode({
								id : 40,
								expanded : true
							}),
					rootVisible : false,
					listeners : {
						'click' : this.clickYylxNode
					}
				});
		var k = new Ext.grid.GridPanel({
					title : "用户列表",
					autoScroll : true,
					id : "contactGrid",
					region : "center",
					height : 370,
					store : i,
					shim : true,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					cm : j,
					sm : c,
					bbar : new Ext.PagingToolbar({
								autoScroll : true,
								height : 40,
								pageSize : 12,
								store : i,
								displayInfo : true,
								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							}),
					listeners : {
						rowdblclick : function() {
							var t = Ext.getCmp("contactGrid");
							var n = Ext.getCmp("selectedUserGrid");
							var u = n.getStore();
							var x = t.getSelectionModel().getSelected();
							var q = x.data.rybh;
							var v = x.data.ryxm;
							var s = false;
							for (var o = 0; o < u.getCount(); o++) {
								if (u.getAt(o).data.rybh == q) {
									s = true;
									break;
								}
							}
							if (!s) {
								var w = {
									rybh : q,
									ryxm : v
								};
								var r = new u.recordType(w);
								n.stopEditing();
								u.add(r);
							}
						}
					}
				});
		var b = new Ext.FormPanel({
					height : 42,
					region : "north",
					layout : "hbox",
					bodyStyle : "padding:6px 2px 2px 2px",
					layoutConfigs : {
						align : "middle"
					},
					defaultType : "label",
					defaults : {
						margins : "0 4 0 4"
					},
					items : [{
								text : "用户姓名"
							}, {
								xtype : "textfield",
								name : "ryxm",
								width : 100
							}, {
								text : "用户警号"
							}, {
								xtype : "textfield",
								name : "rybh",
								width : 50
							}, {
								xtype : "button",
								text : "定位已选",
								// iconCls : "btn-search",
								handler : function() {
									var idx = -1;
									var i = 0;
									a.getStore().each(function(r) {
										i++;
										if (r.data.rybh == b.getForm()
												.findField('rybh').getValue()) {
											idx = i - 1;
											return;
										}
									});
									a.getView().focusRow(idx);
									a.getSelectionModel().selectRow(idx);
								}
							}, {
								text : "完成率(%)"
							}, {
								xtype : "textfield",
								name : "wcl",
								id : "rwwcl",
								value : 60,
								width : 50
							}, {
								xtype : 'checkbox',
								name : 'tksqbz',
								id : 'tksqbz',
								fieldLabel : '已申请',
								checked : true,
								boxLabel : '已申请'
							}, {
								xtype : "button",
								text : "查询",
								// iconCls : "btn-search",
								handler : function() {
									var c = Ext.getCmp("contactGrid");
									var a = c.getStore();			
									a.baseParams = {
										tksqbz : Ext.getCmp('tksqbz').getValue(),
										wcl : Ext.getCmp('rwwcl').getValue()
									};
									b.getForm().submit({
										url : _ctxPath
												+ "/tyksUser4Select.do?op=ajax",
										method : "post",
										success : function(o, p) {
											k.getStore().proxy.conn.url = _ctxPath
													+ "/tyksUser4Select.do";
											var n = Ext.util.JSON
													.decode(p.response.responseText);
											k.getStore().loadData(n);
										},
										failure : function() {
											Ext.Msg.alert("操作信息", "查询失败");
										}
									});
								}
							}]
				});
		var h = new Ext.grid.CheckboxSelectionModel();
		var a = new Ext.grid.GridPanel({
					id : "selectedUserGrid",
					title : "已选用户",
					height : 370,
					columnWidth : .85,
					autoScroll : true,
					store : new Ext.data.ArrayStore({
								fields : ["rybh", "ryxm"],
								listeners : {
									add : function() {
										Ext
												.getCmp("selectedUserGrid")
												.setTitle("已选用户："
														+ this.getCount() + "人");
									},
									remove : function() {
										Ext
												.getCmp("selectedUserGrid")
												.setTitle("已选用户："
														+ this.getCount() + "人");
									}
								}
							}),
					trackMouseOver : true,
					sm : h,
					columns : [h, new Ext.grid.RowNumberer(), {
								header : "用户名",
								dataIndex : "ryxm",
								width : 50
							}, {
								header : "警号",
								dataIndex : "rybh",
								width : 50
							}],
					listeners : {
						rowdblclick : function() {
							var p = Ext.getCmp("selectedUserGrid");
							var q = p.getSelectionModel().getSelected();
							var n = p.getStore();
							p.stopEditing();
							n.remove(q);
						},
						afterrender : function() {
							var u = this.getStore();
							var rybharr = oldrybh.split(",");
							var ryxmarr = oldryxm.split(",");
							if (rybharr[0] != "") {
								for (var p = 0; p < rybharr.length; p++) {
									var q = rybharr[p];
									var v = ryxmarr[p];
									var w = {
										rybh : q,
										ryxm : v
									};
									var r = new u.recordType(w);
									this.stopEditing();
									u.add(r);
								}
							}
						}
					}
				});
		var m = new Ext.Panel({
			width : 200,
			region : "east",
			layout : "column",
			border : false,
			items : [new Ext.Panel({
						frame : true,
						columnWidth : .15,
						height : 400,
						layout : {
							type : "vbox",
							pack : "center",
							align : "stretch"
						},
						defaults : {
							margins : "0 0 5 0"
						},
						items : [{
									xtype : "button",
									iconCls : "add-all",
									text : "添加",
									handler : function() {
										var t = Ext.getCmp("contactGrid");
										var n = Ext.getCmp("selectedUserGrid");
										var u = n.getStore();
										var x = t.getSelectionModel()
												.getSelections();
										for (var p = 0; p < x.length; p++) {
											var q = x[p].data.rybh;
											var v = x[p].data.ryxm;
											var s = false;
											for (var o = 0; o < u.getCount(); o++) {
												if (u.getAt(o).data.rybh == q) {
													s = true;
													break;
												}
											}
											if (!s) {
												var w = {
													rybh : q,
													ryxm : v
												};
												var r = new u.recordType(w);
												n.stopEditing();
												u.add(r);
											}
										}
									}
								}, {
									xtype : "button",
									text : "移除",
									iconCls : "rem-all",
									handler : function() {
										var p = Ext.getCmp("selectedUserGrid");
										var q = p.getSelectionModel()
												.getSelections();
										var n = p.getStore();
										for (var o = 0; o < q.length; o++) {
											p.stopEditing();
											n.remove(q[o]);
										}
									}
								}]
					}), a]
		});
		var g = new Ext.Panel({
					id : "contactPanel",
					width : 540,
					height : 410,
					layout : "border",
					border : false,
					items : [b, {
								region : "west",
								split : true,
								autoScroll : true,
								header : false,
								collapsible : true,
								width : 160,
								layout : "accordion",
								items : [d, yylxPanel]
							}, k]
				});
		if (!e) {
			g.add(m);
		}
		return g;
	},
	clickNode : function(b) {
		if (b != null) {
			var c = Ext.getCmp("contactGrid");
			var a = c.getStore();
			a.proxy.conn.url = _ctxPath + "/tyksUser4Select.do";
			a.baseParams = {
				tksqbz : Ext.getCmp('tksqbz').getValue(),
				wcl : Ext.getCmp('rwwcl').getValue(),
				ids : b.id
			};
			a.load({
						params : {
							start : 0,
							limit : 12
						}
					});
		}
	},
	clickYylxNode : function(b) {
		if (b != null) {
			var c = Ext.getCmp("contactGrid");
			var a = c.getStore();
			a.proxy.conn.url = _ctxPath + "/tyksUser4Select.do";
			a.baseParams = {
				tksqbz : Ext.getCmp('tksqbz').getValue(),
				wcl : Ext.getCmp('rwwcl').getValue(),
				yyids : b.id
			};
			a.load({
						params : {
							start : 0,
							limit : 12
						}
					});
		}
	}
};